package org.example.easybuy.service.impl;

import org.example.easybuy.dao.UserAddressDao;
import org.example.easybuy.entity.UserAddress;
import org.example.easybuy.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressDao userAddressDao;
    @Override
    public boolean addUserAddress(UserAddress userAddress) {
        return userAddressDao.addUserAddress(userAddress)==1;
    }

    @Override
    public List<UserAddress> selectAddressByLoginName(String loginName) {
        return userAddressDao.selectAddressByLoginName(loginName);
    }

    @Override
    public boolean delAddress(int id) {
        return userAddressDao.delAddress(id)==1;
    }

    @Transactional
    @Override
    public boolean changeDefault(int id,int userId) {
        int count=0;
        try{
            if(userAddressDao.changeDefault(id)==1)
            {
                count=userAddressDao.changeNoDefault(id,userId);
            }
        }catch(RuntimeException e)
        {
            throw new RuntimeException();
        }
        return count>0;
    }

    @Override
    public boolean updateUserAddress(UserAddress userAddress) {
        return userAddressDao.updateUserAddress(userAddress)==1;
    }
}
